// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { createStore, Reducer } from "redux"
import { LanguageActionTypes } from "./language/languageActions"
import languageReducer from "./language/languageReducer"
import recommendProductsReducer from "./recommendProducts/recommendProductsReducer"
import thunk, { ThunkMiddleware } from "redux-thunk"
import { actionLog } from "./middlewares/actionLog"
import { productDetailSlice } from "./productDetail/slice"
import { combineReducers, configureStore } from "@reduxjs/toolkit"
import { productSearchSlice } from "./productSearch/slice"
import { userSlice } from "./user/slice"
import { persistStore, persistReducer } from "redux-persist"
import localStorage from "redux-persist/lib/storage"
import { shoppingCartSlice } from "./shoppingCart/slice"
import { orderSlice } from "./order/slice"

const persistConfig = {
  key: "root",
  storage: localStorage,
  whitelist: ["user"],
  blacklist: []
}

const rootReducer = combineReducers({
  language: languageReducer,
  recommendProducts: recommendProductsReducer,
  productDetail: productDetailSlice.reducer,
  productSearch: productSearchSlice.reducer,
  user: userSlice.reducer,
  shoppingCart: shoppingCartSlice.reducer,
  order: orderSlice.reducer
})

const persistedReducer = persistReducer(persistConfig, rootReducer as any) //创建持久化的reducer
// let store = createStore(persistedReducer)
const store = configureStore({
  reducer: persistedReducer,
  // middleware: (getDefaultMiddleware) => [...getDefaultMiddleware(), actionLog],
  devTools: true
})
const persistor = persistStore(store) //创建持久化的store

export type RootState = ReturnType<typeof rootReducer>
export type AppDispatch = typeof store.dispatch

export default { store, persistor }
